package com.bootdo.per.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.per.domain.PersonScoreModel;
import com.bootdo.per.domain.PersonDO;
import com.bootdo.per.domain.TeamDO;
import com.bootdo.per.domain.TeamModel;
import com.bootdo.per.service.ManagerService;
import com.bootdo.per.service.PersonService;
import com.bootdo.per.service.TeamService;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.DeptService;
import com.bootdo.system.service.RoleService;
import com.bootdo.system.service.UserService;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author readke
 * @email 1992lcg@163.com
 * @date 2018-06-05 17:51:30
 */
 
@Controller
@RequestMapping("/per/person")
public class PersonController extends BaseController{
	@Autowired
	private PersonService scoreService;
	@Autowired
	private UserService userService;
	@Autowired
	private ManagerService managerService;
	@GetMapping()
	@RequiresPermissions("per:person:person")
	String person(Model model){
		String status = managerService.list(null).get(0).getStatus();
		model.addAttribute("mStatus", status);
	    return "per/person/person";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("per:person:person")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        query.put("scoreUserId", getUserId());
		List<PersonDO> scoreList = scoreService.listByGroup(query);
		int total = scoreService.listByGroupCount(query);
		PageUtils pageUtils = new PageUtils(scoreList, total);
		return pageUtils;
	}
	
	@GetMapping("/score")
	@RequiresPermissions("per:person:score")
	String score(Model model){
		Integer year = DateUtils.getYear();
		Integer month = DateUtils.getMonth();
		
		model.addAttribute("year", year);
		model.addAttribute("month", month);
		List<UserDO> userList = null;
		UserDO user =  getUser();
		userList = userService.getListByDept(user);
		model.addAttribute("userList", userList);
		
		return "per/person/person_score";
	    
	}
	@ResponseBody
	@PostMapping("/saveScore")
	@RequiresPermissions("per:person:score")
	public R saveScore( PersonScoreModel personScoreModel){

		List<PersonDO> list = personScoreModel.getList();
		for(PersonDO score: list){
			score.setMonth(DateUtils.getMonth());
			score.setYear(DateUtils.getYear());
			score.setQuarter(DateUtils.getQuarter());
			score.setScoreUser(getUsername());
			score.setScoreUserId(getUserId());
			score.setScoreTime(new Date());
		}
		if(scoreService.saveList(list)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	@GetMapping("/view")
	@RequiresPermissions("per:person:view")
	String view(@RequestParam Map<String,Object> map,Model model){
		map.put("scoreUserId", getUserId());
		map.put("sort", "score");
		map.put("order", "desc");
		model.addAttribute("scoreList", scoreService.list(map));
		model.addAttribute("map", map);
		return "per/person/person_score_view";
		
	}
	
	@GetMapping("/edit")
	@RequiresPermissions("per:team:edit")
	String edit(@RequestParam Map<String,Object> map,Model model){
		map.put("scoreUserId", getUserId());
		map.put("sort", "score");
		map.put("order", "desc");
		model.addAttribute("scoreList", scoreService.list(map));
		model.addAttribute("map", map);
		
		return "per/person/person_score_edit";
	}
	@ResponseBody
	@PostMapping("/updateScore")
	@RequiresPermissions("per:team:edit")
	public R updateScore( PersonScoreModel personScoreModel){

		List<PersonDO> list = personScoreModel.getList();
		for(PersonDO score: list){
			score.setModiTime(new Date());
			score.setModiUser(getUsername());
			score.setModiUserId(getUserId());
		}
		if(scoreService.updateList(list)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	@ResponseBody
	@PostMapping("/isScore")
	@RequiresPermissions("per:person:score")
	public R isScore(){

		Integer year = DateUtils.getYear();
		Integer month = DateUtils.getMonth();
		Map<String ,Object> map = new HashMap<String ,Object>();
		map.put("year",year);
		map.put("month",month);
		map.put("scoreUserId",getUserId());
		
		if(scoreService.count(map)>0){
			return R.error();
		}
		return R.ok();
	}
	
}
